﻿using K8.EF;
using KingerRobot.PCRBW.Dao;
using Microsoft.EntityFrameworkCore;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace KingerRobot.PCRBW.SpiService;

public class WcsTaskRepository: IWcsTaskRepository
{

    #region 同步方法
    public  POWcsTask QueryById(long objId)
    {
        var _db = Db.Default;
        return  _db.WcsTasks.FirstOrDefault(d=>d.Id== objId && d.UseFlag==1);
    }
  
    /// <summary>
    /// 功能描述:根据ID查询数据
    /// 作　　者:Robin
    /// </summary>
    /// <param name="lstIds">id列表,如果是联合主键，请使用Where条件</param>
    /// <returns>数据实体列表</returns>
    public  List<POWcsTask> QueryByIDs(object[] lstIds)
    {
        var _db = Db.Default;
        return  _db.WcsTasks.Where(d => lstIds.Contains(lstIds)).ToList();
    }

    /// <summary>
    /// 写入实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns>插入个数 插入成功为1</returns>
    public  int Add(POWcsTask entity)
    {
        var _db = Db.Default;
        return  _db.WcsTasks.Insert(entity);
    }

    /// <summary>
    /// 批量插入实体
    /// </summary>
    /// <param name="listEntity">实体集合</param>
    /// <returns>影响行数</returns>
    public  int Add(IEnumerable<POWcsTask> listEntity)
    {
        var _db = Db.Default;
        _db.WcsTasks.AddRange(listEntity);
        int insertedCount = _db.SaveChanges();
        return insertedCount;
    }
    /// <summary>
    /// 根据实体删除一条数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns></returns>
    public int DeleteById(long id)
    {
        var _db = Db.Default;
        return _db.WcsTasks.Where(d=>d.Id== id).Delete();
    }

    /// <summary>
    /// 删除指定ID集合的数据(批量删除)
    /// </summary>
    /// <param name="ids">主键ID集合</param>
    /// <returns></returns>
    public int Deletes(IEnumerable<POWcsTask> entitys)
    {
        try
        {
            var _db = Db.Default;
            _db.WcsTasks.BulkDelete(entitys);
            int result = _db.SaveChanges();
            return result;
        }
        catch (Exception ex)
        {
            return 0;
        }
    }
    /// <summary>
    /// 更新实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns></returns>
    public  int Update(POWcsTask entity)
    {
        var _db = Db.Default;
        return _db.WcsTasks.Where(d=>d.Id== entity.Id).Update<POWcsTask>(entity);
    }
    /// <summary>
    /// 待条件更新所有数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Update(Expression<Func<POWcsTask, bool>> whereExpression,POWcsTask entity)
    {
        var _db = Db.Default;
        return _db.WcsTasks.Where(whereExpression).Update(entity); 
    }

    /// <summary>
    /// 正序查询第一条数据
    /// </summary>
    /// <returns></returns>
    public POWcsTask First()
    {
        var _db = Db.Default;
        return _db.WcsTasks.First();
    }
    /// <summary>
    /// 查询第一条数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public  POWcsTask First(Expression<Func<POWcsTask, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        var result = _db.WcsTasks.Where(whereExpression).ToList();
        if (result.Count == 0 || result.Count<=0) return null;
        return result.FirstOrDefault();
    }
 

    /// <summary>
    /// 功能描述:查询所有数据
    /// 作　　者:Robin
    /// </summary>
    /// <returns>数据列表</returns>
    public  List<POWcsTask> Query()
    {
        try
        {
            var _db = Db.Default;
            return  _db.WcsTasks.ToList();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Query执行异常:{ex.Message}");
        }
        return null;
    }


    /// <summary>
    /// 功能描述:查询数据列表
    /// 作　　者:Robin
    /// </summary>
    /// <param name="whereExpression">whereExpression</param>
    /// <returns>数据列表</returns>
    public  List<POWcsTask> Query(Expression<Func<POWcsTask, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.WcsTasks.Where(whereExpression).ToList();
    }

    
    /// <summary>
    /// 根据sql语句查询
    /// </summary>
    /// <param name="strSql">完整的sql语句</param>
    /// <param name="parameters">参数</param>
    /// <returns>泛型集合</returns>
    public  List<POWcsTask> QuerySql(string strSql, Parameter[] parameters = null)
    {
        var _db = Db.Default;
        var result=  _db.WcsTasks.FromSqlRaw(strSql, parameters).ToList();

        return result;
    }
    /// <summary>
    /// 根据sql语句查询 执行update、delete、add
    /// </summary>
    /// <param name="strSql">完整的sql语句</param>
    /// <returns>泛型集合</returns>
    public int ExecuteSqlRaw(string strSql, Parameter[] parameters)
    {
        var dbHelper = new DbHelper();       
        dbHelper.CommandText = strSql.ToString();
        dbHelper.CommandType = System.Data.CommandType.Text;
        for (int i = 0; i < parameters.Length; i++)
        {
            dbHelper.AddParameter(parameters[i]);
        }
        int rowsReturned = dbHelper.ExecuteNonQuery();

        return rowsReturned;
    }

    #endregion

    #region 执行事务

    /// <summary>
    /// 执行新增事务
    /// </summary>
    /// <param name="wcsTask"></param>
    /// <param name="wcsTaskCmd"></param>
    /// <returns></returns>
    public int ExecAddTran(POWcsTask wcsTask, POWcsTaskCmd wcsTaskCmd)
    {
        var _db = Db.Default;
        int result = 0;
        try
        {
            //添加事务
            using (var transaction = _db.Database.BeginTransaction())
            {
                result = _db.WcsTasks.Insert(wcsTask);

                result = _db.WcsTaskCmds.Insert(wcsTaskCmd);

                transaction.Commit();
                return wcsTask.TaskNo.GetValueOrDefault();
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"任务执行表：{wcsTask.Id}-创建任务异常:{ex.ToString()}" });
        }
        return result;

    }
    /// <summary>
    /// 执行新增事务
    /// </summary>
    /// <param name="wcsTask"></param>
    /// <param name="wcsTaskCmd"></param>
    /// <returns></returns>
    public int ExecAddWarehouseingTaskTran(POWcsTask wcsTask, POWcsTaskCmd wcsTaskCmd)
    {
        var _db = Db.Default;
        int result = 0;
        try
        {
            //添加事务
            using (var transaction = _db.Database.BeginTransaction())
            {
                result = _db.WcsTasks.Insert(wcsTask);
                result = _db.WcsTaskCmds.Insert(wcsTaskCmd);
                result = _db.RcsLocations.Where(d => d.Id == wcsTask.TargetLoc).Update(new PORcsLocation()
                {
                    Id = wcsTask.TargetLoc,
                    TaskId = wcsTask.Id,
                    TaskOccupy = 1,
                    TaskOccupyTime=DateTime.Now,
                });
                _db.SaveChanges();
                transaction.Commit();
                return wcsTask.TaskNo.GetValueOrDefault();
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"任务执行表：{wcsTask.Id}-创建任务异常:{ex.ToString()}" });
        }
        return result;

    }
    /// <summary>
    /// 执行编辑事务
    /// </summary>
    /// <param name="wcsTask"></param>
    /// <param name="wcsTaskCmd"></param>
    /// <returns></returns>
    public int ExecUpdateTran(POWcsTask wcsTask, POWcsTaskCmd wcsTaskCmd)
    {
        var _db = Db.Default;
        int result = 0;
        try
        {
            //添加事务
            using (var transaction = _db.Database.BeginTransaction())
            {
                result = _db.WcsTasks.Where(d => d.Id == wcsTask.Id).Update(new POWcsTask
                {
                    TaskNo = wcsTask.TaskNo,
                    TaskStatus = wcsTask.TaskStatus,
                    FinishLoc = wcsTask.FinishLoc,
                    FinishMode = wcsTask.FinishMode,
                    FinishTime = wcsTask.FinishTime
                });

                result = _db.WcsTaskCmds.Where(d => d.Id == wcsTaskCmd.Id).Update(new POWcsTaskCmd()
                {
                    CmdStatus = wcsTaskCmd.CmdStatus,
                    FinishLoc = wcsTaskCmd.FinishLoc,
                    FinishMode = wcsTaskCmd.FinishMode,
                    FinishTime = wcsTaskCmd.FinishTime,
                });
                _db.SaveChanges();
                transaction.Commit();
                return result;
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"任务执行表：{wcsTask.Id}-删除异常:{ex.ToString()}" });
        }
        return result;

    }

    /// <summary>
    /// 执行删除事务
    /// </summary>
    /// <param name="wcsTask"></param>
    /// <param name="wcsTaskCmd"></param>
    /// <returns></returns>
    public int ExecDelTran(POWcsTask wcsTask, POWcsTaskCmd wcsTaskCmd)
    {
        var _db = Db.Default;
        int result = 0;
        try
        {
            //添加事务
            using (var transaction = _db.Database.BeginTransaction())
            {
                result = _db.WcsTasks.Where(d => d.Id == wcsTask.Id).Delete();
                result = _db.WcsTaskCmds.Where(d => d.Id == wcsTaskCmd.Id).Delete();
                _db.SaveChanges();
                transaction.Commit();
                return result;
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"任务执行表：{wcsTask.Id}-删除异常:{ex.ToString()}" });
        }
        return result;

    }

   
    #endregion

    #region 获取指令和任务

    /// <summary>
    /// 创建任务
    /// </summary>
    /// <param name="taskDTO"></param>
    /// <returns></returns>
    public POWcsTask CreateTask(TaskDTO taskDTO)
    {
        var startLoc = taskDTO.StartLoc;
        var targetLoc = taskDTO.TargetLoc;
        var type = taskDTO.Type;
        var startEquip = taskDTO.StartEquip;
        var targetEquip = taskDTO.TargetEquip;
        var skuId = taskDTO.SkuId;
        var remark = taskDTO.Remark;
        var taskNo = taskDTO.TaskNo;
        var skuCode = taskDTO.SkuCode;
        var weight = taskDTO.TireWeight;
        var taskGroup = taskDTO.GroupNo;
        var mesId = taskDTO.MesProductId;
        var orderId = taskDTO.OrderId;
        var mark = taskDTO.Mark;
        var Innerdiameter= taskDTO.Innerdiameter;

        if (startLoc == 0 || targetLoc == 0)
        {
            return null;
        }
        if (!Enum.IsDefined(typeof(EnumTaskType), type))
        {
            return null;
        }
     
        if (taskNo == 0)
        {
            taskNo =  K8.LSF.Client.CodeValue(new DateTime(), "TaskNo").ToInt(0);
        }
        POWcsTask pOWcsTask = new()
        {
            Id = XID.Nextval,
            TaskNo = taskNo,
            SkuId = skuId,
            SkuCode = skuCode,
            StartEquip = startEquip,
            StartLoc = startLoc,
            CurrentEquip = startEquip,
            CurrentLoc = startLoc,
            TargetEquip = targetEquip,
            TargetLoc = targetLoc,
            TaskStatus = (int?)EnumTaskStatus.Executable,
            TaskType = type,
            TaskGroup = taskGroup,
            IsAbnormal = false,
            RemarkMemo = remark,
            TooltipMemo = remark,
            MesId = mesId,
            OrderId = orderId,
            Mark = mark,
            MaterialId = taskDTO.material_id,
            Innerdiameter= Innerdiameter.ToString()
        };

        return pOWcsTask;
    }

    /// <summary>
    /// 创建指令
    /// </summary>
    /// <param name="taskDTO">任务数据传输对象</param>
    /// <returns>任务信息</returns>
    public TaskInfoBean CreateTaskAndCmd(TaskDTO taskDTO)
    {

        var task = CreateTask(taskDTO);

        if (task is null)
        {
            return null;
        }

        //TODO  保存下传坐标信息
        var taskCmd = new POWcsTaskCmd()
        {
            Id = XID.Nextval,
            TaskId = task.Id,
            TaskNo = task.TaskNo,
            TaskType = task.TaskType,
            EquipId = task.StartEquip,
            StartLoc = task.StartLoc,
            CurrentLoc = task.CurrentLoc,
            TargetLoc = task.TargetLoc,
            CmdStatus = (int)EnumTaskCmdStatus.Executable,
            CmdType = task.TaskType,
            Mark = task.Mark,
            IsSpray=0,
            Innerdiameter= task.Innerdiameter.ToString()
        };

        return new TaskInfoBean
        {
            POWcsTask = task,
            POWcsTaskCmd = taskCmd
        };
    }

    #endregion

    #region 执行更新任务状态事务
    /// <summary>
    /// 执行更新任务状态事务
    /// </summary>
    /// <param name="wcsTask"></param>
    /// <param name="wcsTaskCmd"></param>
    /// <returns></returns>
    public int ExecUpdateByTaskStatusTran(POWcsTask task, POWcsTaskCmd wcsTaskCmd, int status)
    {
        var _db = Db.Default;
        int result = 0;
        try
        {
            //添加事务
            using (var transaction = _db.Database.BeginTransaction())
            {
                result += _db.WcsTasks.Where(d => d.Id == task.Id).Update(new POWcsTask()
                {
                    Id = task.Id,
                    TaskStatus = status,
                    FinishLoc = task.FinishLoc,
                    FinishMode = task.FinishMode,
                    FinishTime = task.FinishTime,
                });
                result += _db.WcsTaskCmds.Where(d => d.Id == wcsTaskCmd.Id).Update(new POWcsTaskCmd()
                {
                    Id = wcsTaskCmd.Id,
                    CmdStatus = status,
                    FinishLoc = task.FinishLoc,
                    FinishMode = task.FinishMode,
                    FinishTime = task.FinishTime,
                });
                if (status == (int)EnumTaskStatus.Finish)
                {
                    result += _db.BakWcsTasks.CopyFrom(task);
                    result += _db.BakWcsTaskCmds.CopyFrom(wcsTaskCmd);


                    result += _db.WcsTasks.Where(d => d.Id == task.Id).Delete();
                    result += _db.WcsTaskCmds.Where(d => d.Id == wcsTaskCmd.Id).Delete();
                }
                _db.SaveChanges();
                transaction.Commit();
                return result;
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"执行更新表：{task.Id}-更新异常:{ex.ToString()}" });
        }
        return result;

    }
    #endregion

    #region 执行更新任务状态事务
    /// <summary>
    /// 执行备份删除事务
    /// </summary>
    /// <param name="wcsTask"></param>
    /// <param name="wcsTaskCmd"></param>
    /// <returns></returns>
    public int BakAndDeleteData(POWcsTask wcsTask)
    {
        var _db = Db.Default;
        int result = 0;
        try
        {
            var wcsTasks = _db.WcsTasks.FirstOrDefault(d => d.Id == wcsTask.Id);
            var wcsTaskCmds = _db.WcsTaskCmds.FirstOrDefault(d => d.TaskId == wcsTask.Id);
            var bakMesOutOrder = _db.MesOutOrders.FirstOrDefault(d => d.Id == wcsTasks.OrderId);
            //添加事务
            using (var transaction = _db.Database.BeginTransaction())
            {

                result= _db.BakWcsTasks.CopyFrom(wcsTasks);
                result = _db.BakWcsTaskCmds.CopyFrom(wcsTaskCmds);
                result = _db.BakMesOutOrders.CopyFrom(bakMesOutOrder);

                result = _db.WcsTasks.Where(d => d.Id == wcsTask.Id).Delete();
                result = _db.WcsTaskCmds.Where(d => d.TaskId == wcsTask.Id).Delete();
                result = _db.MesOutOrders.Where(d => d.Id == wcsTasks.OrderId).Delete();

                _db.SaveChanges();
                transaction.Commit();
                return result;
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"执行更新表：{wcsTask.Id}-删除异常:{ex.ToString()}" });
        }
        return result;

    }
    #endregion

    public List<TaskCmdAndLocDto> GetTaskAndCmdBeanByCmdEquipId(long equipId)
    {
        List<TaskCmdAndLocDto> allTaskList = null;//所有任务
        List<TaskCmdAndLocDto> abnormalList = null;//所有异常任务
        List<TaskCmdAndLocDto> WarehousingList = null;//入所有库任务
        List<TaskCmdAndLocDto> ExwarehouseList = null;//所有出库任务

        abnormalList = QueryAbnormalList(equipId);//人工出库和返库任务
        WarehousingList = QueryWarehousingList(equipId);//所有的入库代办任务
        ExwarehouseList = QueryExwarehouse(equipId);//所有正常出库的代办任务
                                                    //获取上次执行的任务类型
        Expression<Func<POBakWcsTaskCmd, bool>> whereExpression = x => true;
        if (equipId != 0)
            whereExpression = whereExpression.And(f => f.EquipId == equipId);

        var db = Db.Default;
        var lastTask = (db.BakWcsTaskCmds.Where(whereExpression).OrderByDescending(d => d.FinishTime)).FirstOrDefault();

        //根据设备编码获取可执行任务 且根据上次的任务类型决定下次的任务类型
        if (lastTask is null || lastTask.TaskType == (int)EnumTaskType.Exwarehouse)
        {
            allTaskList = abnormalList.Concat(WarehousingList).Concat(ExwarehouseList).ToList();
        }
        else
        {
            allTaskList = abnormalList.Concat(ExwarehouseList).Concat(WarehousingList).ToList();
        }

        return allTaskList;
    }

    public List<TaskCmdAndLocDto> QueryWarehousingList(long equipId)
    {
        Expression<Func<TaskCmdAndLocDto, bool>> whereExpression = x => true;

        if (equipId != 0)
            whereExpression = whereExpression.And(f => f.WcsTaskCmd.EquipId == equipId);

        var db = Db.Default;
        var dto = (from t in db.WcsTaskCmds
                   join r in db.RcsLocations on t.StartLoc equals r.Id
                   join s in db.RcsLocations on t.TargetLoc equals s.Id
                   where r.UseFlag == 1 && s.UseFlag == 1 && t.CmdStatus == (int)EnumTaskStatus.Executable
                   && t.TaskType == (int)EnumTaskType.Warehousing && t.Mark ==4
                   select new TaskCmdAndLocDto
                   {
                       WcsTaskCmd = t,
                       GetRcsLocation = r,
                       SetRcsLocation = s
                   }).Where(whereExpression).OrderBy(d => d.WcsTaskCmd.CreateTime).ToList();

        return dto;
    }

    public List<TaskCmdAndLocDto> QueryExwarehouse(long equipId)
    {
        Expression<Func<TaskCmdAndLocDto, bool>> whereExpression = x => true;

        if (equipId != 0)
            whereExpression = whereExpression.And(f => f.WcsTaskCmd.EquipId == equipId);

        var db = Db.Default;
        var dto = (from t in db.WcsTaskCmds
                   join r in db.RcsLocations on t.StartLoc equals r.Id
                   join s in db.RcsLocations on t.TargetLoc equals s.Id
                   where r.UseFlag == 1 && t.CmdStatus == (int)EnumTaskStatus.Executable
                            && t.TaskType == (int)EnumTaskType.Exwarehouse && t.Mark == 0
                   select new TaskCmdAndLocDto
                   {
                       WcsTaskCmd = t,
                       GetRcsLocation = r,
                       SetRcsLocation = s
                   }).Where(whereExpression).OrderBy(d => d.WcsTaskCmd.CreateTime).ToList();

        return dto;
    }

    public List<TaskCmdAndLocDto> QueryAbnormalList(long equipId)
    {
        Expression<Func<TaskCmdAndLocDto, bool>> whereExpression = x => true;
        if (equipId != 0)
            whereExpression = whereExpression.And(f => f.WcsTaskCmd.EquipId == equipId);
        var db = Db.Default;
        var dto = (from c in db.WcsTaskCmds
                   join r in db.RcsLocations on c.StartLoc equals r.Id
                   join s in db.RcsLocations on c.TargetLoc equals s.Id
                   where c.CmdStatus == (int)EnumTaskStatus.Executable && c.TaskWmsStatus == (int)EnumTaskCmdStatus.Create && r.UseFlag == 1 &&
                         ((c.TaskType == (int)EnumTaskType.Exwarehouse && (c.Mark == 1 || c.Mark == 2)))
                   select new TaskCmdAndLocDto
                   {
                       WcsTaskCmd = c,
                       GetRcsLocation = r,
                       SetRcsLocation = s
                   }).Where(whereExpression).OrderBy(d => d.WcsTaskCmd.CreateTime).ToList();

        var turnBack = (from c in db.WcsTaskCmds
                        join r in db.RcsLocations on c.StartLoc equals r.Id
                        join s in db.RcsLocations on c.TargetLoc equals s.Id
                        where r.UseFlag == 1 && c.CmdStatus == (int)EnumTaskStatus.Executable && c.TaskWmsStatus == (int)EnumTaskCmdStatus.Create &&
                              ((c.TaskType == (int)EnumTaskType.Warehousing && c.Mark == 3))
                        select new TaskCmdAndLocDto
                        {
                            WcsTaskCmd = c,
                            GetRcsLocation = r,
                            SetRcsLocation = s
                        }).Where(whereExpression).OrderBy(d => d.WcsTaskCmd.CreateTime).ToList();
        var result = turnBack.Concat(dto).ToList();
        return result;
    }

    public List<TaskCmdAndLocDto> GetTrussTaskAndCmdBeanByCmdEquipId(long equipId)
    {
        Expression<Func<TaskCmdAndLocDto, bool>> whereExpression = x => true;

        if (equipId != 0)
            whereExpression = whereExpression.And(f => f.WcsTaskCmd.EquipId == equipId);

        var db = Db.Default;
        var dto = (from t in db.WcsTaskCmds
                   join r in db.RcsLocations on t.StartLoc equals r.Id
                   join s in db.RcsLocations on t.TargetLoc equals s.Id
                   where r.UseFlag == 1 && t.CmdStatus == (int)EnumTaskStatus.Executable
                            && t.TaskType == (int)EnumTaskType.Truss
                   select new TaskCmdAndLocDto
                   {
                       WcsTaskCmd = t,
                       GetRcsLocation = r,
                       SetRcsLocation = s
                   }).Where(whereExpression).OrderBy(d => d.WcsTaskCmd.CreateTime).ToList();

        return dto;
    }


    public TaskAndCmdBean QueryClearTask()
    {
        var db = Db.Default;
        var result = db.WcsTasks
            .FirstOrDefault(t => (t.TaskWmsStatus == 12 || t.TaskWmsStatus == 13 || t.TaskStatus == 9) && t.UseFlag == 1);
        if (result is null)
        {
            return null;
        }
        var cmd = db.WcsTaskCmds.Where(t => t.TaskId == result.Id).FirstOrDefault();

        if (cmd is null) return null;

        return new TaskAndCmdBean
        {
            Task = result,
            Cmd = cmd,
        };

    }

    public int DelTaskAndCmd(TaskAndCmdBean taskCmd)
    {
        var _db = Db.Default;
        int result = 0;
        try
        {
            //添加事务
            using (var transaction = _db.Database.BeginTransaction())
            {
                if (taskCmd.Task.TaskType == (int)EnumTaskType.SlibeTable)
                {
                    _db.MesProducts.Where(d => d.Id == taskCmd.Task.MesId).Update(new MesProduct()
                    {
                        Id = taskCmd.Task.MesId,
                        Status = 0
                    });
                }
                if (taskCmd.Task.TaskType == (int)EnumTaskType.Warehousing)
                {
                    _db.MesProducts.Where(d => d.Id == taskCmd.Task.MesId).Update(new MesProduct()
                    {
                        Id= taskCmd.Task.MesId,
                        Status=5
                    });
                }
                if (taskCmd.Task.TaskType == (int)EnumTaskType.Exwarehouse)
                {
                    _db.MesOutOrders.Where(d => d.Id == taskCmd.Task.OrderId).Update(new MesOutOrder()
                    {
                        Id = taskCmd.Task.OrderId,
                        Status =0
                    });
                }
                if (taskCmd.Task.TaskType == (int)EnumTaskType.Truss)
                {
                    _db.MesOutOrders.Where(d => d.Id == taskCmd.Task.OrderId).Update(new MesOutOrder()
                    {
                        Id = taskCmd.Task.OrderId,
                        Status = 4
                    });
                }
                var wcsTasks = _db.WcsTasks.Where(d => d.Id == taskCmd.Task.Id).Delete();
                var wcsTaskCmds = _db.WcsTaskCmds.Where(d => d.TaskId == taskCmd.Cmd.Id).Delete();

                _db.SaveChanges();
                transaction.Commit();

                return 1;
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"执行更新表：{taskCmd.Task.Id}-删除异常:{ex.ToString()}" });
        }
        return result;

    }
}
